//
//  NewFeatureViewController.m
//  Weibo
//
//  Created by Anasue on 9/11/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "NewFeatureViewController.h"
#import "TabBarViewController.h"

#define NewFeatureImageCount 4

@interface NewFeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation NewFeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupScrollView];
    [self setupPageControl];
}

- (void)setupScrollView
{
    self.view.backgroundColor = [UIColor orangeColor];
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    //设置代理
    scrollView.delegate = self;
    //创建图片
    CGFloat imageY = 0;
    CGFloat imageW = scrollView.width;
    CGFloat imageH = scrollView.height;
    for (int i = 0; i < NewFeatureImageCount; i ++) {
        UIImageView *imageView = [[UIImageView alloc]init];
        NSString *imageName = [NSString stringWithFormat:@"new_feature_%d",i + 1];
        imageView.image = [UIImage imageWithName:imageName];
        CGFloat imageX = imageW * i;
        imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
        [scrollView addSubview:imageView];
        
        imageView.userInteractionEnabled = YES;
        
        //创建按钮
        if (i == NewFeatureImageCount - 1) {
            // 创建最后一张图片
            [self setupLastImageView:imageView];
        }
    }
    
    //可视范围
    scrollView.contentSize =CGSizeMake(NewFeatureImageCount * imageW, 0);
    //背景颜色
    scrollView.backgroundColor = Color(246, 246, 246);
    //清除下面滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
}

- (void)setupLastImageView:(UIImageView *)imageView
{
    [self setupStartButton:imageView];
    [self setupShareButton:imageView];
}
/**
 *  创建分享按钮
 *
 */
- (void)setupShareButton:(UIImageView *)imageView
{
    UIButton *shareButton = [[UIButton alloc]init];
    [shareButton setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareButton setImage:[UIImage imageWithName:@"new_feature_share_true"] forState:UIControlStateSelected];
    shareButton.centerX = self.view.width * 0.28;
    shareButton.centerY = self.view.height * 0.65;
    shareButton.size = CGSizeMake(150, 35);
    shareButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [shareButton setTitle:@"分享到微博" forState:UIControlStateNormal];
    [shareButton addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:shareButton];

}
- (void)shareClick:(UIButton *)shareButton
{
    shareButton.selected = !shareButton.isSelected;
}
/**
 *  创建开启微博按钮
 *
 */
- (void)setupStartButton:(UIImageView *)imageView
{
    UIButton *startButton = [[UIButton alloc]init];
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    startButton.centerX = self.view.width * 0.35;
    startButton.centerY = self.view.height * 0.75;
    startButton.size = startButton.currentBackgroundImage.size;
    [startButton setTitle:@"开启新微博" forState:UIControlStateNormal];
    [startButton addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startButton];


}

- (void)startClick
{
    //进入TabBarViewController
    TabBarViewController *vc = [[TabBarViewController alloc]init];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = vc;
}

/**
 *  创建翻页滚动条
 */
- (void)setupPageControl
{
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    pageControl.numberOfPages = NewFeatureImageCount;
    pageControl.pageIndicatorTintColor = Color(189, 189, 189);
    pageControl.currentPageIndicatorTintColor = Color(253, 98, 43);
    pageControl.centerX= self.view.width * 0.5;
    pageControl.centerY = self.view.height - 40;
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat doublePage = scrollView.contentOffset.x / scrollView.width;
    int intPage = (int)(doublePage + 0.5);
    self.pageControl.currentPage = intPage;
    
    
}









@end
